<template>
  <div class="login-container">
    <div class="logo_l_box">
      <img src="@/assets/logo/logo_n.png" alt="">
    </div>
    <div class="big_bgc">
      <div class="content_center">
        <div class="con_l">
        <ul>
          <li>
            <img src="@/assets/logo/001.png" alt="">
            <div class="w_r_con">
              <p>创新应用“互联网+回收”模式</p>
              <p>助力企业阳光、透明、合规处置</p>
            </div>
          </li>
          <li>
            <img src="@/assets/logo/002.png" alt="">
            <div class="w_r_con">
              <p>30万+竟买服务企业，</p>
              <p>实现买卖双方快速高效、精准匹配</p>
            </div>
          </li><li>
            <img src="@/assets/logo/003.png" alt="">
            <div class="w_r_con">
              <p>建立竞买服务企业诚信评价体系，</p>
              <p>研发0作弊计量核心技术，</p>
              <p>实现安全处置</p>
            </div>
          </li>
          <li>
            <img src="@/assets/logo/004.png" alt="">
            <div class="w_r_con">
              <p>分类回收、创新回收、分解回收,</p>
              <p>拍前评估分类、拍后回收溯源,</p>
              <p>实现循环利用</p>
            </div>
          </li>
        </ul>
      </div>
      <div class="login_r">
        <div class="title-container">
          <h3 class="title">盟拍网设备物资处置系统</h3>
        </div>
        <el-form v-if="loginShow=='1'" ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="账号/手机号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-form>

        <el-form
          v-else
          ref="noteLoginForm"
          :model="noteLoginForm"
          :rules="noteLoginRules"
          class="loginPhoneForm"
          auto-complete="on"
          label-position="left"
          >
          <el-form-item prop="mobile">
              <el-input
              prefix-icon="el-icon-mobile-phone"
              ref="mobile"
              v-model="noteLoginForm.mobile"
              placeholder="手机号"
              name="mobile"
              type="text"
              tabindex="1"
              auto-complete="on"
              clearable
              />
          </el-form-item>
          <el-form-item prop="code">
              <el-input 
              placeholder="请输入验证码" 
              v-model="noteLoginForm.code"
              @keyup.enter.native="loginCodeEvent">
                  <el-button slot="append" :disabled="disabled" @click="getverCode">
                      <span>{{btnTxt}}</span>
                  </el-button>
              </el-input>
          </el-form-item>
        </el-form>

        <div class="login_type">
          <span @click="passWordShow" v-if="loginShow=='2'" class="_hand padding_hh">密码登录</span>
          <span @click="noteShow" v-else class="padding_hh _hand">验证码登录</span>
        </div>

        <div class="btn_box">
          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登 录</el-button>
        </div>
      </div>
      </div>
      
      
    
    </div>
    
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import {getCode} from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'))
      }
      callback()
    }
    const validatePhone = (rule, value, callback) => {
        // 判断输入框中是否输入手机号
        if (!value) {
            callback(new Error('手机号不能为空'))
        }
        //正则表达式进行验证手机号，从1开始，第二位是345789中的任意一位，以9数字结尾
        if (!/^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/.test(value)) {
            callback(new Error('手机号格式不正确'))
        }
        callback()
    }
    return {
      loginShow:'1',  
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      noteLoginForm:{
        mobile: "",
        code: "", 
      },
      noteLoginRules:{
        mobile:[
          { required: true, trigger: 'blur', message: '请输入11位手机号'},
          { required: true, trigger: 'blur', min: 11, max: 11, message: '长度不符合'},
          { required: true, trigger: 'blur', validator: validatePhone}
        ],
        code:[
          { required: true, trigger: 'blur', message: '请输入6位验证码'},
          { required: true, trigger: 'blur', min: 6, max: 6,message: '验证码错误'}    
        ],
      },
      btnTxt: "获取验证码",
      // 是否禁用  即点击之后一段时间内无法再点击
      disabled: false,
      time: 0,
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录按钮
    handleLogin() {
      if(this.loginShow == '1'){
        this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store.dispatch('user/loginAPI', this.loginForm).then(() => {
            this.$router.push({ path: '/' })
          }).catch(() => {
          })
        } else {
          return false
        }
      })
      }else{
        // phoneLogin
        this.$refs.noteLoginForm.validate(valid => {
            if (valid) { 
              this.loading = true
                  //手机号验证码登录
                this.$store.dispatch('user/phoneLogin', this.noteLoginForm).then(() => {
                    // this.$store.dispatch('getUserInfo');
                    this.$router.push({ path: '/' })
                    this.loading = false
                }).catch(() => {
                    this.loading = false
                })
            }
        });
      }

      
    },
    // 
    passWordShow(){
            this.loginShow ='1';
        },
        noteShow(){
            this.loginShow ='2'
        },
    // 手机号登录-验证码 回车键盘事件
    loginCodeEvent(){
        this.$refs.noteLoginForm.validate(valid => {
            if (valid) { 
                  //手机号验证码登录
                this.$store.dispatch('user/phoneLogin', this.noteLoginForm).then(() => {
                    // this.$store.dispatch('getUserInfo');
                    this.$router.push({ path: '/' })
                    // this.loading = false
                }).catch(() => {
                    // this.loading = false
                })
            }
        });
    },
    getverCode(){
      // 校验手机号码
      if (!this.noteLoginForm.phone) {
          //号码校验不通过
          this.$message({
          message: '请输入手机号',
          type:'warning',
          });
          //正则判断 从1开始，第二位是35789中的任意一位，以9数字结尾
      } else if (!/1[35789]\d{9}/.test(this.noteLoginForm.phone)) {
          // 失去焦点后自动触发校验手机号规则
      } else {
          this.time = 60;
          this.disabled = true;
          //调用倒计时方法
          this.timer();
          getCode({mobile:this.noteLoginForm.phone}).then(res=>{
              this.$message({
                  message: res.msg,
                  type: 'success'
              })
          })
      }
    },
    // 倒计时方法
    timer() {
      if (this.time > 0) {
          this.time--;
          // console.log(this.time);
          // this.btnTxt = this.time + "s后重新获取验证码";
          this.btnTxt = "已发送" + this.time + 's';
          setTimeout(this.timer, 1000);
      } else {
          this.time = 0;
          this.btnTxt = "获取验证码";
          this.disabled = false;
      }
    },
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-form {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.5);
    background: rgba(0, 0, 0, 0.5);
    border-radius: 5px;
    color: #454545;
  }
}
.loginPhoneForm{
  .el-input {
    // display: inline-block;
    height: 52px;
    line-height: 52px;
    .el-input-group__append{
      background: rgba(0, 0, 0, 0.5);
      border: 0px;
    }
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      color: $light_gray;
      height: 52px;
    }
  }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.5);
      background: rgba(0, 0, 0, 0.5);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  //background-color: $bg;
  overflow: hidden;
  .big_bgc{
    height: calc(100vh - 100px);
    background-image: url('../../assets/logo/login_n.png');
    background-size: 100% 100%;
    .content_center{
      width: 1300px;
      margin: 0 auto;
      display: flex;
      justify-content: space-between;
    }
    .con_l{
      padding-top: 200px;
      width: 700px;
      ul{
        list-style:none;
        padding: 0;    //去掉默认内边距，也就是ul标签左边的内边距40px；
        margin:0; 
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        li{
          display: flex;
          align-items: center;
          color: #fff;
          margin-top: 70px;
          img{
            width: 90px;
            height: 90px;
          }
          .w_r_con{
            margin-left: 4px;
            p{
              height: 24px;
              line-height: 24px;
            }
          }
        }
      }
    }
  }
  .logo_l_box{
    width: 1300px;
      margin: 0 auto;
    height: 100px;
    background: #fff;
    display: flex;
    align-items: center;
    img{
      width: 150px;
    }
  }
  .loginPhoneForm{
    width: 520px;
    max-width: 100%;
    padding: 10px 35px 0;
    margin: 0 auto;
    
  }

  .btn_box{
    width: 450px;
    margin: 30px auto;
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 10px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .btn_number{
    .el-input{
      display: flex;
    }
    
  }
  .login_type{
      width: 450px;
      margin: 0 auto;
      text-align: right;
      color: #cfc8c8;
      font-size: 14px;
      .padding_hh{
        padding: 6px;
      }
    }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    padding-top: 250px;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
